package screen.window;

import globals.Global;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import screen.item.TextButton;

import attack.PlaceTiles;

public class TilesetWindow extends InterfaceWindow {

	/* Selection */
	int row, col, endRow, endCol;
	
	/* States */
	private boolean selectingTiles;
	
	public TilesetWindow() {
		windowButtons = new ArrayList<TextButton>();
		HEIGHT = 32 * Global.Instance().tileSize + 2 * Global.Instance().INTERFACE_BORDER;
		WIDTH  = 16 * Global.Instance().tileSize + 2 * Global.Instance().INTERFACE_BORDER;
	}
	
	public TilesetWindow(int x, int y) {
		this();
		windowX = x;
		windowY = y;
	}
	
	@Override
	public void Load() {}
	
	@Override
	public void Update(float elapsed) {
		if (windowShow) {
			// Fly down
			if (windowZoom < HEIGHT) windowZoom += HEIGHT/30.0f;
			// Fade in
			if (windowFade < 1.0) windowFade += 0.02f;
			if (windowFade > 1.0) windowFade = 1.0f;
			
			// Input
			if (Global.Instance().input.isKeyReleased('T')) {
				windowShow = false;
			}
			
			// Mouse drag
			if (dragging) {
				windowX += Global.Instance().input.deltaX;
				windowY += Global.Instance().input.deltaY;
			}
			// Mouse over any
			if (Global.Instance().input.mouseX > windowX &&
					Global.Instance().input.mouseX < windowX+WIDTH &&
					Global.Instance().input.mouseY > windowY &&
					Global.Instance().input.mouseY < windowY+HEIGHT) {
				// Over tiles
				if (Global.Instance().input.mouseX > windowX + Global.Instance().INTERFACE_BORDER &&
						Global.Instance().input.mouseX < windowX + WIDTH - Global.Instance().INTERFACE_BORDER &&
						Global.Instance().input.mouseY > windowY + Global.Instance().INTERFACE_BORDER &&
						Global.Instance().input.mouseY < windowY + HEIGHT - Global.Instance().INTERFACE_BORDER) {
					if (Global.Instance().input.leftMouseDown) {
						// Drag on tiles
						if (!selectingTiles) {
							selectingTiles = true;
							int x = Global.Instance().input.mouseX - windowX - Global.Instance().INTERFACE_BORDER;
							int y = Global.Instance().input.mouseY - windowY - Global.Instance().INTERFACE_BORDER;
							
							// Calculate which tile was clicked
							col = x / Global.Instance().tileSize;
							row = y / Global.Instance().tileSize;
						} else {
							int x = Global.Instance().input.mouseX - windowX - Global.Instance().INTERFACE_BORDER;
							int y = Global.Instance().input.mouseY - windowY - Global.Instance().INTERFACE_BORDER;
							
							// Calculate which tile was clicked
							endCol = x / Global.Instance().tileSize;
							endRow = y / Global.Instance().tileSize;
						}
					} else {
						if (selectingTiles) {
							selectingTiles = false;
							
							int w = endCol - col + 1;
							int h = endRow - row + 1;
							int[][] tiles = new int[w][h];
							
							for (int i = 0; i < w; i++) {
								for (int j = 0; j < h; j++) {
									tiles[i][j] = (j + row) + (i + col) * 32;
								}
							}
							
							Global.Instance().leftClick = new PlaceTiles(tiles);
						}
					}
					// Click on tiles
					if (Global.Instance().input.isMouseClicked()) {
						int x = Global.Instance().input.mouseX - windowX - Global.Instance().INTERFACE_BORDER;
						int y = Global.Instance().input.mouseY - windowY - Global.Instance().INTERFACE_BORDER;
						
						// Calculate which tile was clicked
						col = endCol = x / Global.Instance().tileSize;
						row = endRow = y / Global.Instance().tileSize;
						
						// List number
						int tileNum = row + col * 32;
						
						int[][] num = new int[1][1];
						num[0][0] = tileNum;
						Global.Instance().leftClick = new PlaceTiles(num);
					}
				} else if (Global.Instance().input.leftMouseDown) {
					// Drag over border
					dragging = true;
				} else {
					dragging = false;
				}
			}
		} else {
			// Fly up
			if (windowZoom > 0) windowZoom -= HEIGHT/30.0f;
			// Fade out
			if (windowFade > 0) windowFade -= 0.02f;
			if (windowFade < 0) windowFade = 0f;
			
			if (Global.Instance().input.isKeyReleased('T'))
				show();
		}
	}

	@Override
	public void Draw(Graphics graphics) {
		// Draw tileset
		Color c = new Color(0.1f,0.1f,0.1f,windowFade/2);
		graphics.setColor(c);
		graphics.fillRect(windowX, windowY, WIDTH, (int) windowZoom);
		int tilesToDraw = (int) (windowZoom / Global.Instance().tileSize);
		if (tilesToDraw > 32) tilesToDraw = 32;
		for (short i = 0; i < 16; i++)
			for (short j = 0; j < tilesToDraw; j++) {
				graphics.drawImage(
            			// The sprite
            			Global.Instance().tileImages.get(j + i*32),
            			// Screen position rectangle
            			windowX + Global.Instance().tileSize*i + Global.Instance().INTERFACE_BORDER, 
            			windowY + Global.Instance().tileSize*j + Global.Instance().INTERFACE_BORDER,
            			Global.Instance().tileSize,
            			Global.Instance().tileSize,
            			null);
			}
		// Draw tile selection
		float f = windowFade - 0.5f;
		if (f < 0) f = 0f;
		c = new Color(1f,0f,0f,f);
		graphics.setColor(c);
		if (endCol > col || endRow > row) {
			int w = endCol - col;
			int h = endRow - row;
			graphics.fillRect(
					windowX + Global.Instance().INTERFACE_BORDER + col*Global.Instance().tileSize,
					windowY + Global.Instance().INTERFACE_BORDER + row*Global.Instance().tileSize,
					Global.Instance().tileSize * (w+1),
					Global.Instance().tileSize * (h+1));
		} else {
			graphics.fillRect(
					windowX + Global.Instance().INTERFACE_BORDER + col*Global.Instance().tileSize,
					windowY + Global.Instance().INTERFACE_BORDER + row*Global.Instance().tileSize,
					Global.Instance().tileSize,
					Global.Instance().tileSize);
		}
	}
}
